package com.sywb.chuangyebao.widget.riseView;

import android.animation.ValueAnimator;
import android.content.Context;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;
import java.text.DecimalFormat;
public class RiseNumberTextView extends AppCompatTextView implements IRiseNumber{
    private static final int STOPPED=0;
    private static final int RUNNING=1;
    private int mPlayingState = STOPPED;
    private float number;
    private float fromNumber;
    private long duration = 1500;
    private int numberType=2;//1.int 2. float
    private EndListener callback = null;
    final static int[] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,
            99999999, 999999999, Integer.MAX_VALUE };
    private DecimalFormat fnum;
    public RiseNumberTextView(Context context) {
        super(context);
    }
    public RiseNumberTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public RiseNumberTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    /**
     * 判断动画是否正在播放
     * @return
     */
    public boolean isRunning(){
        return (mPlayingState==RUNNING);
    }
    /**
     * 跑小数动画
     */
    private void runFloat(){
        final ValueAnimator valueAnimator = ValueAnimator.ofFloat(fromNumber,number);
        valueAnimator.setDuration(duration);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                //设置瞬时的数据值到界面上
                setText(fnum.format(Float.parseFloat(valueAnimator.getAnimatedValue().toString())));
                if (valueAnimator.getAnimatedFraction()>=1){
                    //设置状态为停止
                    mPlayingState =STOPPED;
                    if (callback!=null){
                         callback.onEndFinish();
                    }
                }
            }
        });
        valueAnimator.start();
    }
    /**
     * 跑整数动画
     */
    private void runInt(){
        ValueAnimator valueAnimator = ValueAnimator.ofInt((int) fromNumber,(int) number);
        valueAnimator.setDuration(duration);
        valueAnimator
                .addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator valueAnimator) {
                        //设置瞬时的数据值到界面上
                        setText(valueAnimator.getAnimatedValue().toString());
                        if (valueAnimator.getAnimatedFraction() >= 1) {
                            //设置状态为停止
                            mPlayingState = STOPPED;
                            if (callback != null)
                                //通知监听器，动画结束事件
                                callback.onEndFinish();
                        }
                    }
                });
        valueAnimator.start();
    }
    static int sizeOfInt(int x) {
        for (int i = 0;; i++){
            if (x <= sizeTable[i])
                return i + 1;
        }
    }
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        fnum = new DecimalFormat("##0.00");
    }
    @Override
    public void start() {
        if (!isRunning()){
            mPlayingState = RUNNING;
            if (numberType==1){
                runInt();
            }else {
                runFloat();
            }
        }
    }
    @Override
    public void withNumber(float number) {
        this.number= number;
        numberType=2;
        if (number>1000){
            fromNumber = number -(float) Math.pow(10,sizeOfInt((int)number)-1);
        }else {
            fromNumber = number/2;
        }
    }
    @Override
    public void withNumber(int number) {
        this.number = number;
        numberType=1;
        if (number>1000){
            fromNumber = number-(float)Math.pow(10,sizeOfInt((int)number)-2);
        }else {
            fromNumber = number/2;
        }
    }
    @Override
    public void setDuration(long duration) {
        this.duration = duration;
    }
    @Override
    public void setOnEndListener(EndListener callback) {
        this.callback = callback;
    }
    /**
     * 定义动画结束接口
     */
    public interface EndListener{
        /**
         * 当动画播放结束时动画
         */
        public void onEndFinish();
    }
}
